-
Notifications
You must be signed in to change notification settings - Fork 232
WIP: Scriptv2 docs #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
WIP: Scriptv2 docs #614
Conversation
79dc0df
to
61862b8
Compare
60061c4
to
ef8d8f7
Compare
@@ -24,7 +39,7 @@ proxy_cache_path /var/run/nginx-cache/jscache levels=1:2 keys_zone=jscache:100m | |||
server { | |||
|
|||
resolver 9.9.9.9; # Use quad9 DNS resolver. Remove this line if you've already configured a DNS resolver. | |||
set $plausible_script_url https://plausible.io/js/script.js; # Change this if you use a different variant of the script | |||
set $plausible_script_url https://plausible.io/js/pa-XXXXX.js; # Change this to path from step 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: maybe remove caching instructions
function queryStrippedUrl() { | ||
return location.href.split('?')[0] | ||
} | ||
|
||
```html | ||
<!-- define the `plausible` function to manually trigger events --> | ||
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script> | ||
``` | ||
|
||
Once that's done, you can create another script in which you will trigger your pageview event. This is where you can override the URL property sent to Plausible and prevent the tracking of UTM tags. To do so, add the following snippet: | ||
|
||
```html | ||
<!-- trigger pageview --> | ||
<script> | ||
function queryStrippedUrl() { | ||
return location.href.split('?')[0] | ||
} | ||
plausible('pageview', { u: queryStrippedURL() }) | ||
</script> | ||
``` | ||
function transformRequest(payload) { | ||
// Sets url for the event manually | ||
payload.u = queryStrippedUrl() | ||
return payload | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Because location may have changed between event collection and the time queryStrippedUrl is called, I suggest we rely on the url in the payload:
function transformRequest(payload) {
var parts = payload.u.split('?')
var urlWithoutQuery = parts.shift()
if (payload.h) {
var fragment = parts.join('?').split('#')[1]
urlWithoutQuery = typeof fragment === 'string'
? urlWithoutQuery + '#' + fragment
: urlWithoutQuery
}
payload.u = urlWithoutQuery
return payload
}
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script> | ||
<script> | ||
```javascript | ||
function redactedUrl() { | ||
var url = window.location.href; | ||
// Replace every all-numeric sequences located between two slashes by "_ID_" | ||
var redactedUrl = url.replace(/\/\d+\//g, "/_ID_/"); | ||
// Send the pageview event to Plausible | ||
plausible('pageview', { u: redactedUrl + window.location.search}); | ||
</script> | ||
return url.replace(/\/\d+\//g, "/_ID_/"); | ||
} | ||
|
||
function transformRequest(payload) { | ||
payload.u = redactedUrl() + window.location.search | ||
return payload | ||
} | ||
|
||
// At the end, update plausible.init call | ||
plausible.init({ | ||
transformRequest: transformRequest | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: This may use stale location, giving inaccurate pageviews, and it also breaks if hashBasedRouting is true. I think we should tune this a bit, along the lines of https://github.com/plausible/docs/pull/614/files#r2314455677
if (canonicalMeta) { | ||
payload.u = canonicalMeta.href + window.location.search | ||
} | ||
return payload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's new:
What's updated:
TBD: